package com.missilike.core.service.impl;

import cn.jiguang.common.ClientConfig;
import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;
import com.missilike.core.domain.JPushProperties;
import com.missilike.core.domain.alipay.AlipayProperties;
import com.missilike.core.service.IJPushService;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpStatus;
import org.springframework.stereotype.Service;

/**
 * @Author: starmile
 * @Description:
 * @Create: 2019-01-10 22:28
 **/
@Slf4j
@Service
public class JPushService implements IJPushService {

    private final JPushProperties jPushProperties;
    public JPushService(JPushProperties jPushProperties){
        this.jPushProperties = jPushProperties;
    }

    /**
     * 快速推送全平台消息
     * @param massage
     * @return
     */
    public Boolean pushAll(String massage) {
        PushPayload pushPayload = PushPayload.alertAll(massage);
        return push(pushPayload);
    }

    /**
     * 推送安卓平台消息
     * @param tag
     * @param massage
     * @param title
     * @return
     */
    public Boolean pushAndroidByTag(String tag, String title, String massage) {
        PushPayload pushPayload = PushPayload.newBuilder()
                .setPlatform(Platform.android())
                .setAudience(Audience.tag(tag))
                .setNotification(Notification.android(massage, title, null))
                .build();
        return push(pushPayload);
    }

    /**
     * 推送ios平台消息
     * @param tag
     * @param massage
     * @param title
     * @return
     */
    public Boolean pushIosByTag(String tag, String title, String massage) {
        PushPayload pushPayload = PushPayload.newBuilder()
                .setPlatform(Platform.ios())
                .setAudience(Audience.tag_and(tag))
                .setNotification(Notification.newBuilder()
                        .addPlatformNotification(IosNotification.newBuilder()
                                .setAlert(title)
                                .setBadge(1)
                                .setSound("happy")
                                .addExtra("from", "JPush")
                                .build())
                        .build())
                .setMessage(Message.content(massage))
                .setOptions(Options.newBuilder()
                        .setApnsProduction(true)
                        .build())
                .build();
        return push(pushPayload);
    }

    /**
     * 推送消息
     * @param payload
     */
    private Boolean push(PushPayload payload) {
        JPushClient jpushClient = new JPushClient(jPushProperties.getMasterSecret(), jPushProperties.getAppKey(), null, ClientConfig.getInstance());

        try {
            PushResult result = jpushClient.sendPush(payload);
            if (result.statusCode == HttpStatus.SC_OK) {
                return true;
            }
            log.info("极光推送结果: " + result);

        } catch (APIConnectionException e) {
            // Connection error, should retry later
            log.error("连接错误，应稍后重试！", e);

        } catch (APIRequestException e) {
            // Should review the error, and fix the request
            log.error("极光推送出现错误！", e);
            log.info("HTTP Status: " + e.getStatus());
            log.info("Error Code: " + e.getErrorCode());
            log.info("Error Message: " + e.getErrorMessage());
        }
        return false;
    }
}
